// tips: WebSocket 调试脚本
var WebSocket = WebSocket || window.WebSocket || window.MozWebSocket;

// 验证浏览器是否支持WebSocket协议
if(!WebSocket) {
	alert("WebSocket not supported by this browser!");
} else {
	var g_ws = null;

	// 载入上次记录的数据
	function Display() {
		console.log("websocket 测试，载入上次记录的数据");
	}

	//日记
	var log = function(s) {
		if(document.readyState !== "complete") {
			log.buffer.push(s);
		} else {
			document.getElementById("contentId").value += (s + "\n"); //打印到文本域
		}
	}

	//连接
	function CreateConnect() {
		var msg = document.getElementById("wsUrlId");
		console.log("CreateConnect(), url: " + msg.value);
		if(g_ws == null) {
			var wsUrlValue = msg.value;
			try {
				g_ws = new WebSocket(wsUrlValue);
				// 监听消息
				g_ws.onmessage = function(event) {
					console.log(eval("(" + event.data + ")")); //实时打印
					log("onmessage(), 接收到服务器消息: " + event.data);
				};
				// 关闭 WebSocket
				g_ws.onclose = function(event) {
					log("onclose(), Socket 已关闭!");
					g_ws = null;
				};
				// 打开WebSocket
				g_ws.onopen = function(event) {
					// 发送一个初始化消息
					log("onopen(), Socket 连接成功!");
				};
				// 打开WebSocket失败
				g_ws.onerror = function(event) {
					log("onerror(), Socket 发生错误!");
				};
			}
			catch(e) {
				g_ws = null;
				log("连接异常, 重置 websocket");
			}
		}
	}

	//发送信息
	function SendMsg() {
		var msg = document.getElementById("messageId");
		var messageObj = { message: msg, type: 'default', };
		var messageJson = JSON.stringify(messageObj);

		if(g_ws != null) {
			log("发送 Socket 消息: " + msg.value);
			g_ws.send(messageJson);
		} else {
			log("Socket 还未创建!, msg: " + msg.value);
		}
	}

	//断开连接
	function CloseConnect() {
		console.log("CloseConnect()");
		if(g_ws != null) {
			g_ws.close();
		}
	}

	//延迟
	function ping() {
		if(g_ws) {
			var messageObj = { type: 'ping' };
			var messageJson = JSON.stringify(messageObj);
			g_ws.send(messageJson);
		}
	}
	setInterval("ping()", 3000);

	//获取关数，次数，命数信息
	function getInfo() {
		var user_id = document.getElementById("user_id");
		var order_id = document.getElementById("order_id");
		var goods_id = document.getElementById("goods_id");

		var messageObj = { type: 'info', user_id: 1249, order_id: 10542, goods_id: 3222 };
		var messageJson = JSON.stringify(messageObj);
		g_ws.send(messageJson);
	}

	//结算
	function done() {
		var messageObj = { type: 'done', order_id: 10542 };
		var messageJson = JSON.stringify(messageObj);
		if(g_ws != null) {
			g_ws.send(messageJson);
		} else {
			log("Socket 还未创建!, msg: " + msg.value);
		}
	}

	//撞到瓶子
	function bottle() {
		var messageObj = { type: 'touch', order_id: 10542, user_id: 1249, goods_id: 3222, bottle_id: 1 };
		var messageJson = JSON.stringify(messageObj);
		if(g_ws != null) {
			g_ws.send(messageJson);
		} else {
			log("Socket 还未创建!, msg: " + msg.value);
		}
	}

	//撞到云
	function cloud() {
		var messageObj = { type: 'cloud', game_id: 1, level_num: 1 };
		var messageJson = JSON.stringify(messageObj);
		if(g_ws != null) {
			g_ws.send(messageJson);
		} else {
			log("Socket 还未创建!, msg: " + msg.value);
		}
	}
	/**
	 * end
	 */
}